package bcontractor.base;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import java.util.HashSet;
import java.util.Set;

import org.junit.Before;
import org.junit.Test;

import bcontractor.api.SentenceSet;
import bcontractor.propositional.PropositionalSentence;
import bcontractor.propositional.SentenceParser;

/**
 * Tests static helper methods on SetUtils
 * 
 * @author lundberg
 * 
 */
public class SetUtilsShould {

    private SentenceParser parser;

    @Before
    public void init() {
        this.parser = new SentenceParser();
    }

    @Test
    public void intersectionOfNoSetsIsEmpty() {
        Set<SentenceSet<PropositionalSentence>> setOfSets = new HashSet<SentenceSet<PropositionalSentence>>();
        assertThat(SetUtils.intersection(setOfSets).isEmpty(), is(true));
    }

    @Test
    public void intersectionOfSingleSetIsTheSingleSet() {
        Set<SentenceSet<PropositionalSentence>> setOfSets = new HashSet<SentenceSet<PropositionalSentence>>();
        setOfSets.add(this.parser.createSet("a", "a v b"));
        assertThat(SetUtils.intersection(setOfSets), is(equalTo(this.parser.createSet("a", "a v b"))));
    }

    @Test
    public void intersectionOfManySetsContainsCommonElements() {
        Set<SentenceSet<PropositionalSentence>> setOfSets = new HashSet<SentenceSet<PropositionalSentence>>();
        setOfSets.add(this.parser.createSet("a", "a v b", "d"));
        setOfSets.add(this.parser.createSet("a", "a v b", "d", "c"));
        setOfSets.add(this.parser.createSet("a v b", "d", "c"));
        assertThat(SetUtils.intersection(setOfSets), is(equalTo(this.parser.createSet("a v b", "d"))));
    }
}
